package com.houhanzhi.strategy;

import com.houhanzhi.enums.ProductOrderPayTypeEnum;
import com.houhanzhi.vo.PayInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class PayFactory {

    @Autowired
    private WechatPayStrategy wechatPayStrategy;

    @Autowired
    private AliPayStrategy aliPayStrategy;

    /**
     * 下单工厂
     * @param payInfoVO
     * @return
     */
    public String pay(PayInfoVO payInfoVO) {
        PayStrategyContext payStrategyContext = getPayStrategyContext(payInfoVO);
        return payStrategyContext.executeUnifiedOrder(payInfoVO);
    }

    /**
     * 退款工厂
     * @param payInfoVO
     * @return
     */
    public String refund(PayInfoVO payInfoVO) {
        PayStrategyContext payStrategyContext = getPayStrategyContext(payInfoVO);
        return payStrategyContext.executeRefund(payInfoVO);
    }

    /**
     * 查询支付状态工厂
     * @param payInfoVO
     * @return
     */
    public String queryPayState(PayInfoVO payInfoVO) {
        PayStrategyContext payStrategyContext = getPayStrategyContext(payInfoVO);
        return payStrategyContext.executeQueryPayState(payInfoVO);
    }

    /**
     * 关单工厂
     * @param payInfoVO
     * @return
     */
    public String closeOrder(PayInfoVO payInfoVO) {
        PayStrategyContext payStrategyContext = getPayStrategyContext(payInfoVO);
        return payStrategyContext.executeCloseOrder(payInfoVO);
    }

    /**
     * 获取策略上下文
     * @param payInfoVO
     * @return
     */
    private PayStrategyContext getPayStrategyContext(PayInfoVO payInfoVO) {
        String payType = payInfoVO.getPayType();
        PayStrategyContext payStrategyContext = null;
        // 支付宝
        if (ProductOrderPayTypeEnum.ALI_PAY.name().equalsIgnoreCase(payType)) {
            payStrategyContext = new PayStrategyContext(aliPayStrategy);
        } else if (ProductOrderPayTypeEnum.WECHAT_PAY.name().equalsIgnoreCase(payType)) {
            // 微信
            payStrategyContext = new PayStrategyContext(wechatPayStrategy);
        }
        return payStrategyContext;
    }
}
